package com.burgeon.backend.WeChat;

import com.burgeon.backend.SHOP.entity.Wechat;
import com.burgeon.backend.SHOP.mappers.WechatAppMapper;
import com.burgeon.backend.Util.menu.WeChatInfo;
import com.burgeon.backend.entity.AccessToken;
import com.burgeon.backend.mappers.WechatMapper;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.security.auth.login.LoginException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class GetAccesToken {

    @Autowired
    private WechatAppMapper wechatAppMapper;

    public String GetAccesToken(String token){
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
        String dateString = sdf.format(now);
        AccessToken oldAccessToken = wechatAppMapper.getAccessToken(token);


        if (null != oldAccessToken && dateString.equals(oldAccessToken.getDate())) {
            return oldAccessToken.getAccessToken();
        }

        String accessToken;
        Wechat wechat = getWechat(token);
        if(null == wechat){
            String grantType = "client_credential";
            String appid = WeChatInfo.WX_APPID;
            String secret =WeChatInfo.WX_APPSECRET;
            String url = "https://api.weixin.qq.com/cgi-bin/token?" +
                    "grant_type=" + grantType +
                    "&appid=" + appid +
                    "&secret=" + secret;
            RestTemplate restTemplate = new RestTemplate();

            try {
                String str = restTemplate.getForObject(url, String.class);
                accessToken = new JSONObject(str).getString("access_token");
            } catch (JSONException e) {
                System.err.println("获取access_token失败");
                return null;
            }
            AccessToken data = new AccessToken();
            data.setAccessToken(accessToken);
            data.setToken(token);
            data.setDate(dateString);

            Integer row = wechatAppMapper.upDataAccessToken(data);
            if (row != 1) {
                //为acc添加accesstoken
                wechatAppMapper.addAccessToken(data);
            }
            return accessToken;
        }
        String grantType = "client_credential";
        String appid = wechat.getAppid();
        String secret = wechat.getAppsecret();
        String url = "https://api.weixin.qq.com/cgi-bin/token?" +
                "grant_type=" + grantType +
                "&appid=" + appid +
                "&secret=" + secret;
        RestTemplate restTemplate = new RestTemplate();

        try {
            String str = restTemplate.getForObject(url, String.class);
            accessToken = new JSONObject(str).getString("access_token");
        } catch (JSONException e) {
            System.err.println("获取access_token失败");
            return null;
        }

        AccessToken data = new AccessToken();
        data.setAccessToken(accessToken);
        data.setToken(token);
        data.setDate(dateString);

        Integer row = wechatAppMapper.upDataAccessToken(data);
        if (row != 1) {
            //为acc添加accesstoken
            wechatAppMapper.addAccessToken(data);
        }
        return accessToken;
    }

    private Wechat getWechat(String token){
        System.err.println(token);
        System.err.println("ni"+wechatAppMapper.getWechat(token));
        return wechatAppMapper.getWechat(token);
    }
}
